package GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;

import ctrLayer.TreeCtr;

/**
 * TreePanel
 * @author Thomas
 */
public class TreePanel extends JPanel {

	private JTable treeTable;
	private JScrollPane scrollPane;
	
	private TreeCtr treeCtr;
	private static TreePanel instance;
	
	public TreePanel() {
		treeCtr = TreeCtr.getInstance();
		
		setLayout(null);
		
		initilizeTreeTable();
		initilizeComponents();
	}
	
	public static TreePanel getInstance() {
		if(instance == null) {
			instance = new TreePanel();
		}
		return instance;
	}
	
	private void initilizeTreeTable() {
		String[][] data = treeCtr.treeListToArray();
		String[] columns = { "Tree nr","Sort", "Lifespan", "CostPrSquareKilometre", "RevenuePrSquareKilometre"};
		
		treeTable = new JTable(data, columns) {
			private static final long serialVersionUID = 1L;
			
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		
		treeTable.getTableHeader().setReorderingAllowed(false);
		treeTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		scrollPane = new JScrollPane(treeTable);
		treeTable.setFillsViewportHeight(true);
		treeTable.setAutoCreateRowSorter(true);
		scrollPane.setBounds(40, 40, 685, 410);
		add(scrollPane);
		
		treeTable.setVisible(true);
		
	}
	
	private void initilizeComponents() {
		JButton btnCreate = new JButton("Create Tree");
		btnCreate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new CreateTreeFrame();
			}
		});
		btnCreate.setBounds(611, 461, 114, 23);
		add(btnCreate);
	}
	
	public void updatePanel(String [][] dataUpdate) {
		treeTable.removeAll();
		this.remove(treeTable);
		scrollPane.removeAll();
		this.remove(scrollPane);
		initilizeTreeTable();
	}
}
